package edu.cuit.zhuyimeng.framework.job.config;

import edu.cuit.zhuyimeng.framework.job.property.PowerJobPasswordProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import tech.powerjob.client.PowerJobClient;
import tech.powerjob.worker.autoconfigure.PowerJobProperties;

/**
 * PowerJob Worker 自动配置类
 */
@AutoConfiguration
@RequiredArgsConstructor
@EnableConfigurationProperties({PowerJobProperties.class, PowerJobPasswordProperties.class})
@ConditionalOnProperty(prefix = "powerjob.worker", name = "enabled", havingValue = "true", matchIfMissing = true)
@Slf4j
public class PowerJobWorkerAutoConfiguration {

    private final PowerJobProperties powerJobProperties;

    private final PowerJobPasswordProperties passwordProperties;

    /**
     *  注册PowerJob OpenAPI 客户端
     * @return PowerJobClient
     */
    @Bean
    public PowerJobClient powerJobClient() {
        log.info("PowerJobClient 开始配置...");
        PowerJobProperties.Worker worker = powerJobProperties.getWorker();
        PowerJobClient client = new PowerJobClient(worker.getServerAddress(),worker.getAppName(), passwordProperties.getPassword());
        log.info("PowerJobClient 加载完毕");
        return client;
    }

}
